;
(function () {
  'use strict';
  angular.module('services.analysisSysOM.throughput')
    .service('avgTpsService', avgTpsService);

  function avgTpsService($http, $filter,CoreService,$timeout,CoreLoading) {
    var time = new Date();
    var date = new Date(time.getFullYear(),time.getMonth(),time.getDate()-1); //页面时间默认当前日期的前一天
    //var date = new Date();
    this.param = {date: date};//页面参数
    var param={};
    this.dataList = [];//数据列表
    this.viewType = 'chart';//视图类型
    this.busLineList = [];//业务线列表
    this.showSearchPanel = true;//显示查询面板
    this.showIcon = true;   //控制显示折线和表格的图标
    this.showArrow = true;  //控制查询面板箭头的显示和隐藏 
    var that = this;
    var chart = null;
    //隐藏查询面板
    this.hideSearchPanel = function () {
      this.showSearchPanel = false;
    };

    this.reset = function () {
      that.showSearchPanel = true;
      that.showIcon = false;
      that.viewType = 'chart';
      that.param.date = date;
      that.busLineList = [];
      that.dataList = [];
    };

    //查询平均tps
    this.search = function () {
      if (!that.showSearchPanel) {
        that.showSearchPanel = true;
        return;
      }
      CoreLoading.setState(true);
      //copy时间 参数date的格式转换
      param = angular.copy(that.param);
      param.date = $filter('date')(param.date, 'yyyy-MM-dd');
      $http.post('/sas/analysisSysOM/sysPerformance/throughput/avgRealtimeTransTPS/query',param)
        .then(function (res) {
          that.showSearchPanel = false;
          if (res.data.state === '10000'&& res.data.data.length >= 1) {
            that.showArrow = true;
            that.showIcon = true; //显示表格和折线图标
            CoreLoading.setState(false);
            var changeList = [];   //转换的数组
            angular.forEach(res.data.data, function (obj) {
              //如果是昨天-23:00（天猫）的数据 则第一条显示
              if (obj.time === '-23:00') {
                changeList.unshift(obj);
              } else {
                changeList.push(obj);
              }

            });
            that.dataList = changeList;
            that.changeViewType();//视图显示类型  无参数即默认折线图
          }else if(res.data.data.length === 0){
            that.PageDisplayStyle();
            CoreService.dialog('查询无数据');
          }else {
            that.PageDisplayStyle();
            CoreService.dialog('查询错误');
          }
        }, function (res) {
        }
      );
    };
    //查询无数据时页面的样式
    this.PageDisplayStyle = function () {
      CoreLoading.setState(false);
      that.showArrow = false;
      that.showSearchPanel = true;
      that.showIcon = false;  //隐藏表格和折线图标
      that.dataList = [];
      that.busLineList = [];
      that.title='';
      chart && chart.clear();
    };

    //初始化图表
    function initChart() {
      chart = echarts.init(document.getElementById('avgTpsChart'));
      var option = {
        animation:false,
        title: {text: $filter('date')(param.date, 'yyyy年MM月dd日') + '统一支付实时交易平均TPS', x: 'center'},
        tooltip: {trigger: 'axis'},
        legend: {data: ['天猫旗舰店', '移动商城', '银行总对总', '有价卡', '灵犀语音'], x: 'right', y: 'center', orient: 'vertical'},
        xAxis: [{type: 'category', boundaryGap: false, data: [], splitLine: {show: false}}],
        yAxis: [{name: '吞吐量（tps）',type: 'value'}],
        series: [
          {name: '天猫旗舰店', type: 'line', data: []},
          {name: '移动商城', type: 'line', data: []},
          {name: '银行总对总', type: 'line', data: []},
          {name: '有价卡', type: 'line', data: []},
          {name: '灵犀语音', type: 'line', data: []}
        ],
        grid: {x: '104', x2: '104'}
      };

      angular.forEach(that.dataList, function (obj) {
        option.xAxis[0].data.push(obj.time);
        option.series[0].data.push(obj.tmall!==null?obj.tmall:0);
        option.series[1].data.push(obj.scpay!==null?obj.scpay:0);
        option.series[2].data.push(obj.bank!==null?obj.bank:0);
        option.series[3].data.push(obj.vces!==null?obj.vces:0);
        option.series[4].data.push(obj.lingxi!==null?obj.lingxi:0);
      });
      chart.setOption(option);
      $timeout(function(){
        that.onSubmit();
      },500);
    }

    //初始化表格业务线列表
    function initTable() {
      that.title = $filter('date')(param.date, 'yyyy年MM月dd日') + '统一支付实时交易平均TPS';
      that.busLineList = [];
      for (var key in that.dataList[0]) {
        //过滤'$$'
        if (key.indexOf('$$') === -1) {
          that.busLineList.push(key.toUpperCase());
        }
      }
    }

    //切换视图
    this.changeViewType = function (type) {
      initChart();
      var ta = document.getElementById('ta');
      var cha = document.getElementById('cha');
      that.viewType = type || that.viewType;
      if (that.viewType === 'chart') {
        ta.style.backgroundColor = '';
        cha.style.backgroundColor = '#2196F3';
        setTimeout(initChart, 0);
      } else if (that.viewType === 'table') {
        ta.style.backgroundColor = '#2196F3';
        cha.style.backgroundColor = '';
        initTable();
      }
    };

    //导出excel
    this.sendData = {date: ''};
    this.onSubmit = function () {
      var data = {
        date: $filter('date')(param.date, 'yyyy-MM-dd'),
        base64s: [chart.getDataURL('png').split(',')[1]],
        title: $filter('date')(param.date, 'yyyy年MM月dd日')  + '统一支付实时交易平均TPS',
        businessLineCode: param.businessLineCode,
        businessLineName: param.businessLineName,
        tradTypeCode: param.tradTypeCode,
        tradTypeName: param.tradTypeName
      };
      that.sendData = JSON.stringify(data);
    };
  }


  avgTpsService.$inject = ['$http', '$filter','CoreService','$timeout','CoreLoading'];
})();